package com.firefly.service.query.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.ums.mapper.MenuMapper;
import com.firefly.dao.dao.ums.po.MenuPo;
import com.firefly.dao.assembler.ums.MenuAssembler;
import com.firefly.dao.dao.ums.service.MenuService;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.vo.ums.BreadCrumbVo;
import com.firefly.dao.vo.ums.MenuTreeVo;
import com.firefly.dao.vo.ums.MenuVo;
import com.firefly.util.utils.JsonUtil;
import com.firefly.util.utils.TreeNodeUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 管理中心权限菜单
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/11/29
 **/
@Service
public class MenuQuery {
    private final MenuService menuService;
    private final MenuMapper menuMapper;
    private final MenuAssembler menuAssembler;
    @Autowired
    public MenuQuery(MenuService menuService, MenuMapper menuMapper, MenuAssembler menuAssembler) {
        this.menuService = menuService;
        this.menuMapper = menuMapper;
        this.menuAssembler = menuAssembler;
    }

    // 当前菜单路径
    public List<BreadCrumbVo> breadCrumb(String url) {
        QueryWrapper<MenuPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", url.substring(1));
        List<BreadCrumbVo> list = new ArrayList<>();
        List<MenuPo> result = menuService.list(queryWrapper);
        if (result.size() > 0){
            list = menuMapper.getAllParent(result.get(0).getId());
        }
        return list;
    }

    // 当前用户的左侧权限菜单
    public List leftMenus(String userMapStr) throws JsonProcessingException {
        QueryWrapper<MenuPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("display", 1).orderByAsc("sort", "id");
        List<MenuPo> list = menuService.list(queryWrapper);
        List<MenuTreeVo> listVo = menuAssembler.getTreeList(list);
        List<MenuTreeVo> result = new ArrayList<>();
        Map userInfo = JsonUtil.str2Obj(userMapStr,Map.class);
        List<Integer> roleIds = (List<Integer>) userInfo.get("roleIds");
        List<String> urls = (List<String>) userInfo.get("urls");
        // 筛选条件
        List<Integer> ageList = Arrays.asList(0);
        // 需要验证权限
        List<MenuTreeVo> tmp = new ArrayList<>();
        tmp = listVo.stream().filter((MenuTreeVo menu) -> ageList.contains(menu.getParentId()) && (roleIds.contains(1) || urls.contains(menu.getUrl()))).collect(Collectors.toList());
        tmp.forEach(item -> {
            result.add(item);
        });
        // 二级
        result.forEach(item -> {
            // 需要验证权限
            List<MenuTreeVo> tmp2 = listVo.stream().filter((MenuTreeVo menu) -> Arrays.asList(((MenuTreeVo) item).getId()).contains(menu.getParentId()) && (roleIds.contains(1) || urls.contains(menu.getUrl()))).collect(Collectors.toList());
            item.setChildren(tmp2);
        });
        // 三级
        result.forEach(item -> {
            item.getChildren().forEach(subItem -> {
                // 需要验证权限
                List<MenuTreeVo> tmp3 = listVo.stream().filter((MenuTreeVo menu) -> Arrays.asList(subItem.getId()).contains(menu.getParentId()) && (roleIds.contains(1) || urls.contains(menu.getUrl()))).collect(Collectors.toList());
                subItem.setChildren(tmp3);
            });
        });
        return result;
    }

    // 所有数据的树形菜单
    public List<Map> listTree() {
        // 查出所有菜单来
        QueryWrapper<MenuPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort", "id");
        List<MenuPo> list = menuService.list(queryWrapper);
        // 类型转换
        List<MenuTreeVo> listTree = menuAssembler.getTreeList(list);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toTree(0, listTree.stream().collect(Collectors.toList()));
        return result;
    }

    // 下拉菜单树
    public List<Map> selectTree() {
        QueryWrapper<MenuPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort", "id").select("id", "parent_id", "name");
        List<MenuPo> list = menuService.list(queryWrapper);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toSelect(0, list.stream().collect(Collectors.toList()));
        return result;
    }

    // 单条信息
    public MenuVo detail(CommonDto request) {
        MenuPo menu = menuService.getById(request.getDetailId());
        MenuVo result = menuAssembler.getVo(menu);
        return result;
    }
}
